<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" :show-close="false" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="userDialogFormRef" :model="state.userForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户">
							<el-input v-model="state.userForm.name" placeholder="请输入账户名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="昵称">
							<el-input v-model="state.userForm.nicename" placeholder="请输入用户昵称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色">
							<el-select v-model="state.userForm.role" placeholder="请选择" clearable class="w100">
								<el-option label="管理员" value="admin"></el-option>
								<el-option label="普通用户" value="common" selected></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态">
							<el-switch v-model="state.userForm.status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref } from 'vue';
import { useradd } from "@/api/api"
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	userForm: {
    id:null,
    name: '', // 账户名称
    nicename: '', // 用户昵称
		role: '', // 关联角色
    status: true, // 用户状态
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row) => {
	if (type === 'edit') {
    // console.log(row.status)
		state.userForm = row;
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
    // console.log(state.userForm.status)
	} else {
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
    state.userForm.status=true
    state.userForm.nicename=''
    state.userForm.name=''
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	userDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	const  res= await useradd(state.userForm)
	closeDialog();
	emit('refresh');
	state.userForm.id=null
	// if (state.dialog.type === 'add') { }
};


// 暴露变量
defineExpose({
	openDialog,
});
</script>
